<template>
  <div>
    <!--Breadcrumb面包屑组件-->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="10">
          <el-input
            placeholder="请输入查找内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe style="width: 100%">
        <el-table-column type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column
          prop="order_price"
          label="订单价格"
          width="100"
        ></el-table-column>
        <el-table-column prop="order_pay" label="支付状态" width="100">
          <template slot-scope="scope">
            <el-tag
              :type="tableTypeAndText.order_pay[scope.row.pay_status].type"
              >{{
                tableTypeAndText.order_pay[scope.row.pay_status].text
              }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="is_send"
          label="是否发货"
          width="100"
        ></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | formatDate }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <!--修改地址-->
            <el-button
              @click="editLocation(scope.row)"
              type="primary"
              icon="el-icon-edit"
              circle
            ></el-button>
            <!--物流信息-->
            <el-button
              @click="displayProgress(scope.row)"
              type="success"
              icon="el-icon-map-location"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[8, 15, 20, 25]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog
      :visible.sync="editLocationDialog"
      title="修改地址"
      @close="editLocationDialogClose"
    >
      <el-form
        label-width="90px"
        :model="editLocationForm"
        :rules="editLocationFormRules"
        ref="editLocationFormRef"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            clearable
            v-model="selectedKeys"
            :options="cityData"
            :props="cateCascaderProp"
            @change="selectedChange"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="primary" @click="editLocationDialog = false"
          >确 定</el-button
        >
        <el-button @click="editLocationDialog = false">取 消</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :visible.sync="displayProgressDialog"
      title="物流信息"
      width="50%"
    >
      <!--timestamp时间线的时间-->
      <!--:reverse排序正序倒序，默认正序false-->
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(item, index) in progressData"
          :key="index"
          :timestamp="item.time"
        >
          <!--时间事件内容-->
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityData from "./citydata"; // 引入省市区数据
export default {
  name: "orders",
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 15,
        user_id: "",
        pay_status: "",
        is_send: "",
        order_fapiao_title: "",
        order_fapiao_company: "",
        order_fapiao_content: "",
        consignee_addr: ""
      },
      cityData,
      selectedKeys: [],
      progressData: [],
      displayProgressDialog: false,
      editLocationForm: {
        address1: [],
        address2: ""
      },
      editLocationFormRules: {
        address1: [
          {
            required: true,
            message: "请选择省市区/县",
            trigger: "blur"
          }
        ],
        address2: [
          {
            required: true,
            message: "请填写详细地址",
            trigger: "blur"
          }
        ]
      },
      cateCascaderProp: {
        expandTrigger: "hover",
        label: "label",
        value: "value",
        children: "children"
      },
      total: 0,
      tableTypeAndText: {
        order_pay: [
          {
            type: "danger",
            text: "未支付"
          },
          {
            type: "success",
            text: "已支付"
          }
        ]
      },
      orderList: [],
      editLocationDialog: false
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取订单数据失败，请重试!");
      this.total = res.data.total;
      this.orderList = res.data.goods;
      console.log(this.orderList);
      this.$message.success("获取订单数据成功！");
    },
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize;
      this.getOrderList();
    },
    handleCurrentChange(current) {
      this.queryInfo.pagenum = current;
      this.getOrderList();
      console.log(this.queryInfo);
    },
    editLocation(row) {
      this.editLocationDialog = true;
    },
    selectedChange() {
      this.editLocationForm.address1 = this.selectedKeys;
    },
    editLocationDialogClose() {
      this.$refs.editLocationFormRef.resetFields();
    },
    async displayProgress() {
      const { data: res } = await this.$http.get("/kuaidi/1106975712662");
      if (res.meta.status !== 200)
        return this.$message.error("该订单物流信息获取失败，请重试!");
      this.progressData = res.data;
      this.displayProgressDialog = true;
    }
  },
  computed: {}
};
</script>

<style scoped lang="less">
.el-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>
